# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
豆瓣app-电影-长评
账号:18241255868
密码:gkx5868
一次位移20.最多480 共计500条
"""

class DouBan_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent()
        self.headers = {
            # "User-Agent": ua.random
            'User-Agent': 'api-client/1 com.douban.frodo/6.30.0(177) Android/21 product/CHE-TL00H vendor/HUAWEI model/CHE-TL00H  rom/android  network/wifi  platform/mobile'

        }


    def get_comments(self, current):
        postData = {
            'rtype':'review',
            'start':current,
            'count':'20',
            'version':'0',
            'order_by':'hot',
            'os_rom':'android',
            'apikey':'0dad551ec0f84ed02907ff5c42e8ec70',
            'channel':'Douban',
            'udid':'032004f87cc3d15ed12b6fb54f8ca1c925284b06',
            '_sig':'Shm4nPPV2XVvh0uLYiUVqxR+HvY=',
            '_ts':'1583203007'
        }
        url = f"https://frodo.douban.com/api/v2/movie/26794435/reviews"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        for d in data_json['reviews']:
            dict_data = dict()
            dict_data['subject'] = d['subject']['title']  # 电影名
            dict_data['title'] = d['title']  # 标题
            dict_data['abstract'] = d['abstract']  # 内容
            dict_data['create_time'] = d['create_time']  # 时间
            dict_data['star_count'] = d['rating']['star_count']  # 评价几星
            dict_data['comments_count'] = d['comments_count']  # 回复
            dict_data['useful_count'] = d['useful_count']  # 有用
            dict_data['reshares_count'] = d['reshares_count']  # 转发
            dict_data['user_id'] = d['user']['id']  # 用户ID
            dict_data['user'] = d['user']['name']  # 用户名
            dict_data['user_url'] = d['user']['url']  # 用户连接

            print(str(dict_data))


if __name__ == '__main__':
    douban = DouBan_Get()
    for i in range(0, 500, 20):
        print(f"=========={i}=============")
        douban.get_comments(i)